(function(){
	var loginBtn = '<div id="login_btn" class="login_hit_all" style="display: none;">\
		<div class="cover_bg"></div>\
		<div class="cover_box">\
			<div class="cover_center login_hit_box">\
				<a class="colse_btn">×</a>\
				<div class="login_hit_header">\
					<h5>会员登录</h5>\
					<span>还没有账号？<a>立即注册</a></span>\
				</div>\
				<div class="login_input_box">\
					<input type="text" name="username" placeholder="账号" />\
				</div>\
				<div  class="login_input_box">\
					<input type="password" name="pwd" placeholder="密码" />\
				</div>\
				<div class="login_label_box">\
					<a class="non_password">忘记密码？</a>\
				</div>\
				<div class="login_btn_box">\
					<p class="tip"></p>\
					<a class="login_btn">登录</a>\
				</div>\
			</div>\
		</div>\
	</div>';
	
	function initEvent(){
		$(".login_btn",loginDom).click(loginServer);
		$(".colse_btn",loginDom).click(close);
	}
	
	var loginDom = $("#login_btn");
	var errorDom = null;
	
	function login(func){
		if(loginDom.length == 0){
			loginDom = $(loginBtn)
			errorDom = $(".tip",loginDom)
			loginDom.appendTo(document.body);
			initEvent();
		}
		$(".login_btn",loginDom).data("callback",null);
		errorDom.text("");
		loginDom.show();
		if(typeof func == "function"){
			$(".login_btn",loginDom).data("callback",func);
		}
	}
	
	function close(){
		loginDom.hide();
	}
	
	function loginServer(){
		errorDom.text("")
		var username = $(".login_input_box input[name='username']",loginDom).val();
		var pwd = $(".login_input_box input[name='pwd']",loginDom).val();
		if(!username){
			errorDom.text("用户名不能为空");
			return;
		}
		if(!pwd){
			errorDom.text("密码不能为空");
			return;
		}
		pwd = hex_md5(pwd);
		sendReq(ctx+"/user/login2",{username:username,pwd:pwd},function(data){
			var status =data.status;
			if(status == true){
				close();
				$(".top_left_box").html('<a href="'+ctx+'/user/toMyMaterial" >'+username+'</a><a href="'+ctx+'/user/logout" >注销</a>');
				var func = $(".login_btn",loginDom).data("callback");
				$(".login_btn",loginDom).data("callback",null);
				func();
			}else{
				errorDom.text(data.msg)
			}
		})
	}
	
	window.login = login;
	
})();